package com.xtool.livewallpaper.mood

import android.appwidget.AppWidgetManager
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES
import com.gyf.immersionbar.ImmersionBar
import com.xtool.livewallpaper.databinding.ActivityMoodWallpaperBinding

class MoodWallpaperActivity : AppCompatActivity() {
    private val binding: ActivityMoodWallpaperBinding by lazy {
        ActivityMoodWallpaperBinding.inflate(
            layoutInflater
        )
    }
    private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID

    override fun onStart() {
        super.onStart()
        val uiMode = resources.configuration.uiMode
        val isNightMode = AppCompatDelegate.getDefaultNightMode() == MODE_NIGHT_YES
                || uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
        ImmersionBar.with(this)
            .statusBarDarkFont(!isNightMode)
            .transparentStatusBar()
            .init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setResult(RESULT_CANCELED)
        setContentView(binding.root)
        val resultValue = Intent()
        val intent = intent
        val extras = intent.extras
        if (extras != null) {
            appWidgetId = extras.getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID
            )
        }
        if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish()
            return
        }
        binding.btnAdd.setOnClickListener {
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
            setResult(RESULT_OK, resultValue)
            finish()
        }
    }
}